﻿using AntdUI;

namespace TestPressure
{
    public partial class FormPointManageForm : BaseForm
    {

        #region

        FormPoint? obj;

        #endregion

        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<FormPoint> list = SqlSugarHelper.Inquireable<FormPoint>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<FormPoint> list)
        {
            var antItemList = new List<FormPointItem>();
            foreach (FormPoint item in list)
            {
                antItemList.Add(new FormPointItem(item.form_point_number, item.form_point_name, item.device_point_number, item.device_point_name));
            }
            Table.DataSource = antItemList;
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckRowExist(int number)
        {
            obj = ObjectWithPrimaryKey(number);
            if (obj == null)
            {
                LoadList();
                string msg = String.Format("编号【{0}】的界面点位不存在", number);
                Error(msg,this);
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>根据主键查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private FormPoint? ObjectWithPrimaryKey(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["form_point_number"] = number;
            List<FormPoint> list = SqlSugarHelper.Inquireable<FormPoint>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region 系统方法
        public FormPointManageForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            FullScreen = true;
        }

        private void FormPointManageForm_Load(object sender, EventArgs e)
        {
            Header.Text = "界面点位管理";
            Table.Height = Height - Header.Height - StackPanel1.Height;
            Table.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("form_point_number","界面点位编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("form_point_name","界面点位名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("device_point_number","设备点位编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("device_point_name","设备点位名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("operation","操作",AntdUI.ColumnAlign.Center){Width="120"}
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table.Spin(config, () =>
            {
                LoadList();
            }, () =>
            {

            });
        }
        #endregion

        #region 响应事件
        private void AddButton_Click(object sender, EventArgs e)
        {
            FormPointAddForm frm = new FormPointAddForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        /// <summary>点击表格操作按钮
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Table_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is FormPointItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    bool res = CheckRowExist(data.form_point_number);
                    if (!res)
                    {
                        return;
                    }

                    string msg = string.Format("您确定要删除【{0}】点位吗？", data.form_point_name);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            if (obj is not null)
                            {
                                List<FormPoint> list = new List<FormPoint>();
                                list.Add(obj);
                                int deleteRes = SqlSugarHelper.Deleteable(list);
                                if (deleteRes > 0)
                                {
                                    LoadList();
                                }
                                else
                                {
                                    Error("删除点位失败",this);
                                }
                            }
                            else
                            {
                                Error("删除点位失败", this);
                            }
                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
